package com.bbt.dormassistant.service.impl;

import com.bbt.dormassistant.dao.UserInformationMapper;
import com.bbt.dormassistant.entity.UserInformation;
import com.bbt.dormassistant.service.UserInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInformationServiceImpl implements UserInformationService {

    @Resource
    private UserInformationMapper userInformationMapper;

    /**
     *
     * @param userInformation 用户信息
     * @return Boolean
     */
    @Override
    public Boolean addUserInformation(UserInformation userInformation) {
        try {
            if (userInformationMapper.selectByPrimaryKey(userInformation.getUserId()) != null) return false;
            userInformation.setIsManger( (byte) 0 );
            return userInformationMapper.insert(userInformation) == 1;
        }catch (Exception e){
            return false;
        }
    }

    /**
     *
     * @param userId 用户ID
     * @return UserInformation
     */
    @Override
    public UserInformation queryUserInformationById(String userId){
        return userInformationMapper.selectByPrimaryKey(userId);
    }


    /**
     * @param userId 用户ID
     * @param name 姓名
     * @param nickName 昵称
     * @param gender 性别
     * @param number 学号
     * @param department 学院
     * @param hobby 兴趣
     * @param building 宿舍楼栋
     * @param room 房间号
     * @param wakeTime 起床时间
     * @param sleepTime 睡眠时间
     * @param avatarUrl 头像地址
     * @return Boolean
     */
    @Override
    public Boolean modifyUserInformation(String userId,
                                         String name,
                                         String nickName,
                                         Integer gender,
                                         String number,
                                         String department,
                                         String hobby,
                                         String building,
                                         Integer room,
                                         String wakeTime,
                                         String sleepTime,
                                         String avatarUrl) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
            Date wake = simpleDateFormat.parse(wakeTime);
            Date sleep = simpleDateFormat.parse(sleepTime);

            if(gender != 0 && gender != 1) return false;

            UserInformation userInformation = userInformationMapper.selectByPrimaryKey(userId);
            userInformation.setName(name);
            userInformation.setNickname(nickName);
            userInformation.setSex(gender);
            userInformation.setNumber(number);
            userInformation.setDepartment(department);
            userInformation.setHobby(hobby);
            userInformation.setBuilding(building);
            userInformation.setRoom(room);
            userInformation.setWakeTime(wake);
            userInformation.setSleepTime(sleep);
            userInformation.setAvatarUrl(avatarUrl);
            return userInformationMapper.updateByPrimaryKey(userInformation) == 1;

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     *
     * @param currIndex 当前页
     * @param pageSize 页大小
     * @return List<UserInformation>
     */
    @Override
    public List<UserInformation> queryAllUserInformations(Integer currIndex,Integer pageSize) {
        Map<String,Object> data = new HashMap<>();
        data.put("currIndex",currIndex==null? 0:(currIndex-1)*pageSize);
        data.put("pageSize",pageSize==null? 1000:pageSize);
        return userInformationMapper.selectAll(data);
    }




}
